# -*- coding: utf-8 -*-
from .models import *
from .utils import *
from .signals import *
import re
import pandas as pd
from metlib.shell.fileutil import *
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, \
    HttpResponseNotFound, Http404, \
    HttpResponseServerError, HttpResponseNotAllowed, \
    HttpResponseBadRequest
from metlib.misc.datatype import Null
from metlib.misc.misc import gen_uuid8, parse_bool
from weblib.django.http.response import file_response
from weblib.django.setting import get_setting
from keeper.workers import *
import time
import collections


def timestamp_datetime(value, format=None):
    if format is None:
        format = '%Y-%m-%d %H:%M:%S'
    # value为传入的值为时间戳(整形)，如：1332888820
    value = time.localtime(value)
    ## 经过localtime转换后变成
    ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
    # 最后再经过strftime函数转换为正常日期格式。
    dt = time.strftime(format, value)
    return dt


def datetime_timestamp(dt):
    # dt为字符串
    # 中间过程，一般都需要将字符串转化为时间数组
    time.strptime(dt, '%Y-%m-%d %H:%M:%S')
    ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)
    # 将"2012-03-28 06:53:40"转化为时间戳
    s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
    return int(s)


#
class Data_csv(object):
    @staticmethod
    def pt_csv(request, dataset, *args, **kwargs):
        """查询接口(lookup)的Django View,
        Finalized on 20150825
        :param request:
        :param args:
        :param kwargs:
        :return: 查找结果的JsonResponse
        """
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        info["type"] = info.pop("type", "point")
        # enddt = info["enddt"]
        # distance = info["distance"]
        result = data_keeper.lookup(info, request=request, *args, **kwargs)
        for re in result:
            if re["dataset"] == dataset:
                param = re["version_replace_dict"][re["version_default"][0]]
        datatime_list = []
        pd_dict = collections.OrderedDict()
        for var_height in vars:
            # gw-c9a3/PT/rh/2016/70/6_199_098
            var, height = var_height.split("_")
            uri = "%s/PT/%s/%s/%s/%s" % (dataset, var, year, height, param)
            get_dict = {"ignore_cache": info.pop('ignore_cache', False)}
            filepath = data_keeper.data_json(request, uri, get_dict, *args, **kwargs)
            datajson = json.loads(open(filepath, 'rb').read())
            data = datajson["contents"][uri]["data"]
            if len(datatime_list) == 0:
                for dt in data["dts"]:
                    datatime_list.append(timestamp_datetime(dt))
                # datatime_list.reverse()
                pd_dict["datatime"] = datatime_list
            pd_dict[var_height] = data["values"]

        dt = pd.DataFrame(pd_dict)
        return dt, param, filepath


class Data_json(object):
    @staticmethod
    def pt_json(request, dataset, *args, **kwargs):
        """查询接口(lookup)的Django View,
        Finalized on 20150825
        :param request:
        :param args:
        :param kwargs:
        :return: 查找结果的JsonResponse
        """
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        info["type"] = info.pop("type", "point")
        # enddt = info["enddt"]
        # distance = info["distance"]
        result = data_keeper.lookup(info, request=request, *args, **kwargs)
        json_data = {}
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    json_data[version] = {}
                    # param = re["version_replace_dict"][re["version_default"][0]]
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    param = re["version_replace_dict"][version]
                    datatime_list = []
                    for var_height in vars:
                        # gw-c9a3/PT/rh/2016/70/6_199_098
                        var, height = var_height.split("_")
                        uri = "%s/PT/%s/%s/%s/%s" % (dataset, var, year, height, param)
                        get_dict = {"ignore_cache": info.pop('ignore_cache', False)}
                        filepath = data_keeper.data_json(request, uri, get_dict, *args, **kwargs)
                        datajson = json.loads(open(filepath, 'rb').read())
                        data = datajson["contents"][uri]["data"]
                        json_data[version][var_height] = collections.OrderedDict()
                        for idx, dt in enumerate(data["dts"]):
                            json_data[version][var_height][timestamp_datetime(dt, format="%Y%m%d%H%M%S")] = \
                                data["values"][idx]
                            # datatime_list.reverse()
                            # json_data[version]["datatime"] = datatime_list
                            # json_data[version][var_height] = data["values"]

        return json_data

    @staticmethod
    def py_json(request, dataset, *args, **kwargs):
        """查询接口(lookup)的Django View,
        Finalized on 20150825
        :param request:
        :param args:
        :param kwargs:
        :return: 查找结果的JsonResponse
        """
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        info["type"] = info.pop("type", "point")
        # enddt = info["enddt"]
        # distance = info["distance"]
        result = data_keeper.lookup(info, request=request, *args, **kwargs)
        json_data = {}
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    json_data[version] = {}
                    # param = re["version_replace_dict"][re["version_default"][0]]
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    param = re["version_replace_dict"][version]
                    datatime_list = []
                    for var_height in vars:
                        # gw-c9a3/PT/rh/2016/70/6_199_098
                        var, height = var_height.split("_")
                        uri = "%s/PT/%s/%s/%s/%s" % (dataset, var, year, height, param)
                        get_dict = {"ignore_cache": info.pop('ignore_cache', False)}
                        filepath = data_keeper.data_json(request, uri, get_dict, *args, **kwargs)
                        datajson = json.loads(open(filepath, 'rb').read())
                        data = datajson["contents"][uri]["data"]
                        json_data[version][var_height] = collections.OrderedDict()
                        for idx, dt in enumerate(data["dts"]):
                            json_data[version][var_height][timestamp_datetime(dt, format="%Y%m%d%H%M%S")] = \
                                data["values"][idx]
                            # datatime_list.reverse()
                            # json_data[version]["datatime"] = datatime_list
                            # json_data[version][var_height] = data["values"]

        return json_data

    @staticmethod
    def pm_json(request, dataset, *args, **kwargs):
        """查询接口(lookup)的Django View,
        Finalized on 20150825
        :param request:
        :param args:
        :param kwargs:
        :return: 查找结果的JsonResponse
        """
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        info["type"] = info.pop("type", "point")
        # enddt = info["enddt"]
        # distance = info["distance"]
        result = data_keeper.lookup(info, request=request, *args, **kwargs)
        json_data = {}
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    json_data[version] = {}
                    # param = re["version_replace_dict"][re["version_default"][0]]
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    param = re["version_replace_dict"][version]
                    datatime_list = []
                    for var_height in vars:
                        # gw-c9a3/PT/rh/2016/70/6_199_098
                        var, height = var_height.split("_")
                        uri = "%s/PT/%s/%s/%s/%s" % (dataset, var, year, height, param)
                        get_dict = {"ignore_cache": info.pop('ignore_cache', False)}
                        filepath = data_keeper.data_json(request, uri, get_dict, *args, **kwargs)
                        datajson = json.loads(open(filepath, 'rb').read())
                        data = datajson["contents"][uri]["data"]
                        json_data[version][var_height] = collections.OrderedDict()
                        for idx, dt in enumerate(data["dts"]):
                            json_data[version][var_height][timestamp_datetime(dt, format="%Y%m%d%H%M%S")] = \
                                data["values"][idx]
                            # datatime_list.reverse()
                            # json_data[version]["datatime"] = datatime_list
                            # json_data[version][var_height] = data["values"]

        return json_data

    @staticmethod
    def pd_json(request, dataset, *args, **kwargs):
        """查询接口(lookup)的Django View,
        Finalized on 20150825
        :param request:
        :param args:
        :param kwargs:
        :return: 查找结果的JsonResponse
        """
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        info["type"] = info.pop("type", "point")
        # enddt = info["enddt"]
        # distance = info["distance"]
        result = data_keeper.lookup(info, request=request, *args, **kwargs)
        json_data = {}
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    json_data[version] = {}
                    # param = re["version_replace_dict"][re["version_default"][0]]
        for re in result:
            if re["dataset"] == dataset:
                for version in re["versions"]:
                    param = re["version_replace_dict"][version]
                    datatime_list = []
                    for var_height in vars:
                        # gw-c9a3/PT/rh/2016/70/6_199_098
                        var, height = var_height.split("_")
                        uri = "%s/PT/%s/%s/%s/%s" % (dataset, var, year, height, param)
                        get_dict = {"ignore_cache": info.pop('ignore_cache', False)}
                        filepath = data_keeper.data_json(request, uri, get_dict, *args, **kwargs)
                        datajson = json.loads(open(filepath, 'rb').read())
                        data = datajson["contents"][uri]["data"]
                        json_data[version][var_height] = collections.OrderedDict()
                        for idx, dt in enumerate(data["dts"]):
                            json_data[version][var_height][timestamp_datetime(dt, format="%Y%m%d%H%M%S")] = \
                                data["values"][idx]
                            # datatime_list.reverse()
                            # json_data[version]["datatime"] = datatime_list
                            # json_data[version][var_height] = data["values"]

        return json_data


def data_to_cvs_file(request, uri, *args, **kwargs):
    """查询接口(lookup)的Django View,
    Finalized on 20150825
    :param request:
    :param args:
    :param kwargs:
    :return: 查找结果的JsonResponse
    """
    try:
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        # enddt = info["enddt"]
        key = uri.split('/', 2)
        dataset = key[1]
        funcname = key[0]
        data_csv = Data_csv()
        func = getattr(data_csv, funcname)
        dt, param, filepath = func(request, dataset, *args, **kwargs)
        csv_path_file = "%s_%s.csv" % (filepath, "csv")
        dt.to_csv(csv_path_file, index=False)
        save_name = "%s-%s-%s-%s.csv" % (funcname, year, ",".join(vars), param)
        return file_response(csv_path_file, save_name=save_name, remove_on_finish=True)

    except Exception as e:
        return ErrorJsonResponse({"status": "Error", "type": unicode(type(e)), "reason": unicode(e)})


def data_to_json(request, uri, *args, **kwargs):
    """查询接口(lookup)的Django View,
    Finalized on 20150825
    :param request:
    :param args:
    :param kwargs:
    :return: 查找结果的JsonResponse
    """
    try:
        if request.method not in ('GET', 'POST'):
            return HttpResponseNotAllowed(['GET', 'POST'])
        if request.method == 'GET':
            info = request.GET.dict()
        else:
            info = request.POST.dict()
        kwargs['user'] = request.user
        vars = info["vars"].split(",")
        year = info["year"]
        # enddt = info["enddt"]
        key = uri.split('/', 2)
        dataset = key[1]
        funcname = key[0]
        data_csv = Data_json()
        func = getattr(data_csv, funcname)
        json_data = func(request, dataset, *args, **kwargs)
        return JsonResponse(json_data)

    except Exception as e:
        return ErrorJsonResponse({"status": "Error", "type": unicode(type(e)), "reason": unicode(e)})


def data_view(request, uri, *args, **kwargs):
    try:
        key = uri.split('/', 2)
        funcname = key[0]
        if funcname[-4:] == "json":
            return data_to_json(request, uri, *args, **kwargs)
        elif funcname[-3:] == "csv":
            return data_to_cvs_file(request, uri, *args, **kwargs)
    except Exception as e:
        return ErrorJsonResponse({"status": "Error", "type": unicode(type(e)), "reason": unicode(e)})
